/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bankcomm.zxing.view;

import com.bankcomm.R;
import com.bankcomm.zxing.camera.CameraManager;
import com.google.zxing.ResultPoint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import java.util.Collection;
import java.util.HashSet;

/**
 * This view is overlaid on top of the camera preview. It adds the viewfinder
 * rectangle and partial transparency outside it, as well as the laser scanner
 * animation and result points.
 * 
 * @author dswitkin@google.com (Daniel Switkin)
 */
public final class ViewfinderView extends View {

	private static final int[] SCANNER_ALPHA = { 0, 64, 128, 192, 255, 192,
			128, 64 };
	private static final long ANIMATION_DELAY = 100L;
	private static final int OPAQUE = 0xFF;

	private final Paint paint;
	private Bitmap resultBitmap;
	private final int maskColor;
	private final int resultColor;
	private final int frameColor;
	private final int laserColor;
	private final int resultPointColor;
	private int scannerAlpha;
	private Collection<ResultPoint> possibleResultPoints;
	private Collection<ResultPoint> lastPossibleResultPoints;

	public boolean isQCodeBar = false;
	private Rect[] rects = null;
	// This constructor is used when the class is built from an XML resource.
	public ViewfinderView(Context context, AttributeSet attrs) {
		super(context, attrs);

		// Initialize these once for performance rather than calling them every
		// time in onDraw().
		paint = new Paint();
		Resources resources = getResources();
		maskColor = resources.getColor(R.color.viewfinder_mask);
		resultColor = resources.getColor(R.color.result_view);
		frameColor = resources.getColor(R.color.viewfinder_frame);
		laserColor = resources.getColor(R.color.viewfinder_laser);
		resultPointColor = resources.getColor(R.color.possible_result_points);
		scannerAlpha = 0;
		possibleResultPoints = new HashSet<ResultPoint>(5);
	}

	@Override
	public void onDraw(Canvas canvas) {
		Rect frame = CameraManager.get().getFramingRect();
		if (frame == null) {
			return;
		}
		int width = canvas.getWidth();
		int height = canvas.getHeight();
		
		int fLeftRight = (width - frame.width())/2;
		if(fLeftRight <= 0){
			fLeftRight = 20;
		}
		int fTopBottom = (height - frame.height())/2;
		//画中间的横线或者是十字线
		drawQRBarCode(canvas,frame,fLeftRight,fTopBottom);				
		
		// Draw the exterior (i.e. outside the framing rect) darkened
		paint.setColor(resultBitmap != null ? resultColor : maskColor);
		canvas.drawRect(0, 0, width, fTopBottom, paint);
		canvas.drawRect(0, height - fTopBottom,width,height,paint);
		canvas.drawRect(0,fTopBottom,fLeftRight,height-fTopBottom,paint);
		canvas.drawRect(width-fLeftRight,fTopBottom,width,height-fTopBottom,paint);

		if (resultBitmap != null) {
			paint.setAlpha(OPAQUE);
			canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);
		} else {
			drawFourRect(canvas,frame,fLeftRight,fTopBottom);
			
			Collection<ResultPoint> currentPossible = possibleResultPoints;
			Collection<ResultPoint> currentLast = lastPossibleResultPoints;
			if (currentPossible.isEmpty()) {
				lastPossibleResultPoints = null;
			} else {
				possibleResultPoints = new HashSet<ResultPoint>(5);
				lastPossibleResultPoints = currentPossible;
				paint.setAlpha(OPAQUE);
				paint.setColor(resultPointColor);
				for (ResultPoint point : currentPossible) {
					canvas.drawCircle(frame.left + point.getX(), frame.top
							+ point.getY(), 6.0f, paint);
				}
			}
			
			if (currentLast != null) {
				paint.setAlpha(OPAQUE / 2);
				paint.setColor(resultPointColor);
				for (ResultPoint point : currentLast) {
					canvas.drawCircle(frame.left + point.getX(), frame.top
							+ point.getY(), 3.0f, paint);
				}
			}
			postInvalidateDelayed(ANIMATION_DELAY, fLeftRight, fTopBottom,width- fLeftRight, height - fTopBottom);
		}
	}
	
	/**
	 * 画四个直角 add by cheng.l
	 * @param canvas
	 * @param frame
	 * @param fLeftRight
	 * @param fTopBottom
	 */
	private void drawFourRect(Canvas canvas, Rect frame, int left,
			int top) {
		// TODO Auto-generated method stub
		int padding = 15;
		int h = 40;
		int w = 6;
		int bh = h - w;
		int width = canvas.getWidth();
		int height = canvas.getHeight();
		
		paint.setColor(frameColor);
		if(null == rects){
			rects = new Rect[8];
			rects[0] = new Rect(left+padding, top+padding, left+padding+w, top+padding+h);
			rects[1] = new Rect(width - left - padding - w, top+padding, width-left-padding, top+padding+h);
			rects[2] = new Rect(left+padding, height - top - padding - h, left+padding+w, height - top - padding);
			rects[3] = new Rect(width - left - padding - w,height - top - padding - h, width-left-padding, height - top - padding);
			
			rects[4] = new Rect(left+padding+w, top+padding, left+padding+bh, top+padding+w);
			rects[5] = new Rect(width - left - padding - h, top+padding, width-left-padding-w, top+padding+w);
			rects[6] = new Rect(left+padding+w, height - top - padding - w, left+padding+bh, height - top - padding);
			rects[7] = new Rect(width - left - padding - h,height - top - padding - w, width-left-padding, height - top - padding);
		}
		
		for(int i = 0;i < rects.length;i++){
			canvas.drawRect(rects[i], paint);			
		}
	}

	/**
	 * 画中间的十字加号,画中间的横条 add by cheng.l
	 * @param canvas
	 * @param frame
	 * @param left
	 * @param top 
	 */
	public void drawQRBarCode(Canvas canvas,Rect frame,int left,int top){
		paint.setColor(laserColor);
		paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
		scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
		
		int rw = 40;
		int rh = 6;
		
		int hmiddle = canvas.getWidth()/2;
		int vmiddle = canvas.getHeight()/2;
		
		if(isQCodeBar){
			Rect hRect = new Rect(hmiddle - rw/2, vmiddle - rh/2, hmiddle+rw/2, vmiddle + rh/2);
			Rect vRect = new Rect(hmiddle - rh/2, vmiddle - rw/2, hmiddle+rh/2, vmiddle + rw/2);
			canvas.drawRect(hRect, paint);
			canvas.drawRect(vRect, paint);			
		}else{
			rw = canvas.getWidth() - 2*left-30;
			canvas.drawRect(hmiddle - rw/2, vmiddle - rh/2,hmiddle + rw/2, vmiddle + rh/2, paint);
		}
	}
	
	public void drawViewfinder() {
		resultBitmap = null;
		invalidate();
	}

	public void addPossibleResultPoint(ResultPoint point) {
		possibleResultPoints.add(point);
	}

}
